/**
 * 
 */
package com.xbang.core.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xbang.core.dao.search.component.SearchComponent;
import com.xbang.core.dao.support.GenericDaoSupport;
import com.xbang.core.page.Page;
import com.xbang.core.page.PageUtils;
import com.xbang.core.page.Result;
import com.xbang.core.service.BaseService;
import com.xbang.core.utils.SearchUtils;

/**
 * @author dev.zhang
 * 
 */
@Service("baseService")
public class BaseServiceImpl implements BaseService {

	public static Logger logger;

	public BaseServiceImpl() {
		logger = Logger.getLogger(getClass());
	}

	@Autowired
	protected GenericDaoSupport genericDaoSupport;

	public void merge(Object object) throws Exception {
		genericDaoSupport.getHibernateTemplate().merge(object);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.xbang.core.service.BaseService#getObject(java.lang.Class, java.io.Serializable)
	 */
	public Object getObject(Class clazz, Serializable id) throws Exception {
		return genericDaoSupport.get(clazz, id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.xbang.core.service.BaseService#getObjects(java.lang.Class)
	 */
	public List getObjects(Class clazz) throws Exception {
		return genericDaoSupport.loadAll(clazz);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.xbang.core.service.BaseService#save(java.lang.Object)
	 */
	public Serializable save(Object object) throws Exception {
		return genericDaoSupport.save(object);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.xbang.core.service.BaseService#update(java.lang.Object)
	 */
	public void update(Object object) throws Exception {
		try{
			genericDaoSupport.update(object);
		} catch (Exception e) {
			genericDaoSupport.getHibernateTemplate().merge(object);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tudou.core.service.BusinessService#saveOrUpdate(java.lang.Object)
	 */
	public void saveOrUpdate(Object object) {
		genericDaoSupport.saveOrUpdate(object);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tudou.core.service.BusinessService#delete(java.lang.Object)
	 */
	public void delete(Object object) {
		genericDaoSupport.delete(object);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tudou.core.service.BusinessService#delete(java.lang.Class, java.io.Serializable)
	 */
	public void delete(Class persistentClass, Serializable id) {
		genericDaoSupport.delete(persistentClass, id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tudou.core.service.BusinessService#load(java.lang.Class, java.io.Serializable)
	 */
	public Object load(Class persistentClass, Serializable id) {
		return genericDaoSupport.load(persistentClass, id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tudou.core.service.BusinessService#get(java.lang.Class, java.io.Serializable)
	 */
	public Object get(Class persistentClass, Serializable id) {
		return genericDaoSupport.get(persistentClass, id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tudou.core.service.BusinessService#loadAll(java.lang.Class)
	 */
	public List loadAll(Class persistentClass) {
		return genericDaoSupport.loadAll(persistentClass);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tudou.core.service.BusinessService#count(java.lang.Class)
	 */
	public int count(Class persistentClass) {
		return genericDaoSupport.count(persistentClass);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tudou.core.service.BusinessService#list(java.lang.Class, int, int)
	 */
	public List list(Class persistentClass, int beginIndex, int maxResult) {
		return genericDaoSupport.list(persistentClass, beginIndex, maxResult);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tudou.core.service.BusinessService#listByPage(java.lang.Class, com.tudou.core.page.Page)
	 */
	public Result listByPage(Class persistentClass, Page page) {
		List result = new ArrayList();
		int totalRecords = genericDaoSupport.count(persistentClass);
		if (totalRecords > 0) {
			page = PageUtils.createPage(page, totalRecords);
			result = genericDaoSupport.list(persistentClass, page.getBeginIndex(), page.getEveryPageSize());
		}
		return new Result(page, result);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tudou.core.service.BusinessService#searchForInt(java.lang.String, java.util.Map)
	 */
	public int searchForInt(String sentence, Map parameters) {
		return genericDaoSupport.searchForInt(sentence, parameters);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tudou.core.service.BusinessService#searchForList(java.lang.String, java.util.Map)
	 */
	public List searchForList(String sentence, Map parameters) {
		return genericDaoSupport.searchForList(sentence, parameters);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tudou.core.service.BusinessService#searchForList(java.lang.String, java.util.Map, int, int)
	 */
	public List searchForList(String sentence, Map parameters, int beginIndex, int maxResult) {
		return genericDaoSupport.searchForList(sentence, parameters, beginIndex, maxResult);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tudou.core.service.BusinessService#searchByPage(java.lang.String, java.util.Map,
	 * com.tudou.core.page.Page)
	 */
	public Result searchByPage(String sentence, Map parameters, Page page) {
		List result = new ArrayList();
		int totalRecords = genericDaoSupport.searchForInt(SearchUtils.getHQLCountSentence(sentence), parameters);
		if (totalRecords > 0) {
			page = PageUtils.createPage(page, totalRecords);
			result = genericDaoSupport.searchForList(sentence, parameters, page.getBeginIndex(), page.getEveryPageSize());
		}
		return new Result(page, result);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tudou.core.service.BusinessService#searchForList(java.lang.String,
	 * com.tudou.core.search.SearchComponent)
	 */
	public List searchForList(String sentenceTemplate, SearchComponent searchComponent) {
		return searchForList(searchComponent.getSearchSentence(sentenceTemplate, true), searchComponent.getParameters());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tudou.core.service.BusinessService#searchByPage(java.lang.String, com.tudou.core.search.SearchComponent,
	 * com.tudou.core.page.Page)
	 */
	public Result searchByPage(String sentenceTemplate, SearchComponent searchComponent, Page page) {
		List result = new ArrayList();
		int totalRecords = searchForInt(searchComponent.getHQLCountSentence(sentenceTemplate, true), searchComponent.getParameters());
		if (totalRecords > 0) {
			page = PageUtils.createPage(page, totalRecords);
			result = genericDaoSupport.searchForList(searchComponent.getSearchSentence(sentenceTemplate, true), searchComponent.getParameters(), page
					.getBeginIndex(), page.getEveryPageSize());
		}
		return new Result(page, result);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tudou.core.service.BusinessService#queryForInt(java.lang.String, java.util.Map)
	 */
	public int queryForInt(String sentence, Map parameters) {
		return genericDaoSupport.queryForInt(sentence, parameters);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tudou.core.service.BusinessService#queryForInt(java.lang.String, java.lang.Object)
	 */
	public int queryForInt(String sentence, Object parameterBean) {
		return genericDaoSupport.queryForInt(sentence, parameterBean);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tudou.core.service.BusinessService#queryForList(java.lang.String, java.util.Map)
	 */
	public List queryForList(String sentence, Map parameters) {
		return genericDaoSupport.queryForList(sentence, parameters);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tudou.core.service.BusinessService#queryForList(java.lang.String, java.util.Map, int, int)
	 */
	public List queryForList(String sentence, Map parameters, int beginIndex, int maxResult) {
		return genericDaoSupport.queryForList(sentence, parameters, beginIndex, maxResult);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tudou.core.service.BusinessService#queryByPage(java.lang.String, java.util.Map,
	 * com.tudou.core.page.Page)
	 */
	public Result queryByPage(String sentence, Map parameters, Page page) {
		List result = new ArrayList();
		int totalRecords = genericDaoSupport.queryForInt(SearchUtils.getSQLCountSentence(sentence), parameters);
		if (totalRecords > 0) {
			page = PageUtils.createPage(page, totalRecords);
			result = genericDaoSupport.queryForList(sentence, parameters, page.getBeginIndex(), page.getEveryPageSize());
		}
		return new Result(page, result);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tudou.core.service.BusinessService#queryForList(java.lang.String, java.lang.Object)
	 */
	public List queryForList(String sentence, Object parameterBean) {
		return genericDaoSupport.queryForList(sentence, parameterBean);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tudou.core.service.BusinessService#queryForList(java.lang.String, java.util.Map, java.lang.Class)
	 */
	public List queryForList(String sentence, Map parameters, Class resultClass) {
		return genericDaoSupport.queryForList(sentence, parameters, resultClass);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tudou.core.service.BusinessService#queryForList(java.lang.String, java.lang.Object, java.lang.Class)
	 */
	public List queryForList(String sentence, Object parameterBean, Class resultClass) {
		return genericDaoSupport.queryForList(sentence, parameterBean, resultClass);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.xbang.core.service.BaseService#execute(java.lang.String, java.util.Map)
	 */
	public void execute(String sqlSentence, Map parameters) throws Exception {
		genericDaoSupport.getJdbcTemplate().update(sqlSentence, parameters);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tudou.core.service.BusinessService#queryForList(java.lang.String, com.tudou.core.search.SearchComponent)
	 */
	public List queryForList(String sentenceTemplate, SearchComponent searchComponent) {
		return genericDaoSupport.queryForList(searchComponent.getSearchSentence(sentenceTemplate, true), searchComponent.getParameters());
	}

	/**
	 * @param genericDaoSupport
	 */
	public void setGenericDaoSupport(GenericDaoSupport genericDaoSupport) {
		this.genericDaoSupport = genericDaoSupport;
	}
}
